package nc.pub.filesystem.newui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;

import javax.swing.JComboBox;

import nc.bs.pub.filesystem.FSOption;
import nc.bs.pub.filesystem.IFileSystemService;
import nc.pub.billcode.ui.ButtonPanel;
import nc.pub.filesystem.newui.FileMoveDialog.MoveFolder;
import nc.ui.ml.NCLangRes;
import nc.vo.pub.BusinessException;
import nc.vo.pub.filesystem.NCFileNode;
import nc.vo.pub.filesystem.NCFileVO;

public class FileCopyDialog extends FileMoveDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public FileCopyDialog(FileContentPanel container) {
		super(container);
		setTitle("Copy to");
	}

	@Override
	public ButtonPanel getBtnpanel() {
		ButtonPanel panel = super.getBtnpanel();
		clearOkActionListeners(panel);
		addOkActionCopyListener(panel);
		return panel;
	}

	private void addOkActionCopyListener(ButtonPanel panel) {
		panel.getBtnOK().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent event) {
				int row = fielcontentpanel.getPatchInfoTable().getSelectedRow();
				if (row == -1) {
					return;
				}
				NCFileVO filevo = (NCFileVO) fielcontentpanel.getfiletablemodel().getVO(row);
				String oldpath = filevo.getFullPath();
				String filenodepath = filevo.getParentpath();
				String oldtreepath = "";

				if (filevo.getParentpath().equalsIgnoreCase(fielcontentpanel.getRootDirStr())) {
					oldtreepath = "root";
				} else {
					oldtreepath = filevo.getParentpath();
				}
				String destDirPath ="";
				String filenewpath = ((FileCopyDialog.MoveFolder)getComboxfolder().getSelectedItem()).getValue();
				if (filenewpath.endsWith("/")) {
					destDirPath = filenewpath + filevo.getName();
				} else {
					destDirPath = filenewpath + "/" + filevo.getName();
				}

				if (oldpath.equalsIgnoreCase(destDirPath)) {
					nc.ui.pub.beans.MessageDialog.showErrorDlg(FileCopyDialog.this,
							NCLangRes.getInstance().getStrByID("sftemp", "UploadURLAction-0005"),
							NCLangRes.getInstance().getStrByID("sftemp", "FileMoveDialog-0002"));
					return;
				}
//				filevo.setPath(destDirPath);

				NCFileNode node = (NCFileNode) fielcontentpanel.getPatchTree().getSelectionPath()
						.getLastPathComponent();
				IFileSystemService service = (IFileSystemService) nc.bs.framework.common.NCLocator.getInstance()
						.lookup(IFileSystemService.class);
				try {
//					boolean b = service.updateFileAttribute(filevo, oldpath);
//					String newpath = destDirPath.substring(21, destDirPath.length() - 1);
					service.copyTo(filevo.getFullPath(), filenewpath, FSOption.WHEN_DEST_EXIST_STOP);
					filevo.setPath(destDirPath);
//					((Collection) fielcontentpanel.getTreenodefilecacheMap().get(filenodepath)).remove(filevo);
					ArrayList<NCFileVO> arr = new ArrayList();
					arr.addAll((Collection) fielcontentpanel.getTreenodefilecacheMap().get(filevo.getParentpath()));
					arr.add(filevo);

					fielcontentpanel.getTreenodefilecacheMap().put(filevo.getParentpath(), arr);
					NCFileVO[] arrtablevo = (NCFileVO[]) ((Collection) fielcontentpanel.getTreenodefilecacheMap()
							.get(oldtreepath)).toArray(new NCFileVO[0]);
					fielcontentpanel.getfiletablemodel().clearTable();
					if (node.getFullPath().equals(fielcontentpanel.rootDirStr)) {
						fielcontentpanel.getfiletablemodel()
								.addVO((nc.vo.pub.ValueObject[]) ((Collection) fielcontentpanel
										.getTreenodefilecacheMap().get("root")).toArray(new NCFileVO[0]));
					} else {
						fielcontentpanel.getfiletablemodel()
								.addVO((nc.vo.pub.ValueObject[]) ((Collection) fielcontentpanel
										.getTreenodefilecacheMap().get(node.getFullPath()))
												.toArray(new NCFileVO[0]));
					}

					fielcontentpanel.refreshStatuBar();
					
					closeOK();
				} catch (BusinessException e1) {
					nc.bs.logging.Logger.error(e1);
				}
			}
		});
	}

	private void clearOkActionListeners(ButtonPanel panel) {
		ActionListener[] listeners = panel.getBtnOK().getActionListeners();
		if (listeners != null && listeners.length > 0) {
			for (ActionListener l : listeners) {
				panel.getBtnOK().removeActionListener(l);
			}
		}
	}

}
